<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\BusinessException;
use App\Models\BannerModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class HomeController extends ApiController
{
    const HOME_CACHE_KEY = 'hbHomeList';

    /**
     * 获取首页推荐列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getList(Request $request): JsonResponse
    {
        $position = $request->input('position', 1);
        $Model = BannerModel::where('position', $position)->where('status', 1);
        if ($request->input('title'))
            $Model = $Model->where('title', $request->input('title'));
        return $this->successPaginate($Model->paginate(10));
    }

    /**
     * 首页
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        if (!Cache::has(self::HOME_CACHE_KEY . $request->input('area_id', 1)))
            self::setCache($request->input('area_id', 1));
        return $this->success(Cache::get(self::HOME_CACHE_KEY . $request->input('area_id', 1)));
    }

    /**
     * 获取开机视频
     * @return JsonResponse
     */
    public function getBootVideo(): JsonResponse
    {
        $video = BannerModel::where('position', 4)->first();
        if ($video)
            $video->video = url( 'storage/' . $video->video);
        return $this->success($video);
    }

    /**
     * 获取以图搜图广告
     * @return JsonResponse
     */
    public function getImageSearchAd(): JsonResponse
    {
        $data = BannerModel::where('position', 5)->first();
        if ($data) {
            $data->image = $data->image ? url( 'storage/' . $data->image) : '';
            $data->video = $data->video ? url( 'storage/' . $data->video) : '';
        }
        return $this->success($data);
    }

    /**
     * 添加/编辑
     * @param Request $request
     * @return JsonResponse
     */
    public function doSave(Request $request): JsonResponse
    {
        $data = $request->only(['id', 'type', 'image', 'video', 'url', 'sp_id', 'pid', 'allmedia', 'position']);
        if ($request->input('position')) {
			 if ($request->input('id'))
				BannerModel::where('id', $request->input('id'))->update($data);
			else
				BannerModel::create($data);
			@file_get_contents('https://apicenter.toysmodel.cn/api/65e4798f833c5');
        }
        return $this->success();
    }

    /**
     * 删除
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doDelete(): JsonResponse
    {
        $id = $this->verifyId('id');
        BannerModel::where('id', $id)->delete();
		@file_get_contents('https://apicenter.toysmodel.cn/api/65e4798f833c5');
        return $this->success();
    }

    /**
     * 设置缓存
     * @param int $area_id
     * @return void
     */
    public static function setCache(int $area_id): void
    {
        $collection = BannerModel::where('position', '>=', 1)->where('position', '<=', 3)
            ->where('area', $area_id)->get()->each((function ($item) {
                $item->image = $item->image ? url( 'storage/' . $item->image) : '';
                $item->video = $item->video ? url( 'storage/' . $item->video) : '';
                $item->cid = 0; // 删除
                return $item;
            }));
        $data = [];
        $positionDict = [1 => 'banner', 2 => 'notice', 3 => 'supply'];
        foreach ($positionDict as $position => $positionText) {
            $positionCollection = $collection->where('position', $position);
            $data[$positionText] = array_values($positionCollection->toArray());
        }
        Cache::put(self::HOME_CACHE_KEY . $area_id, $data);
    }
}
